home *** CD-ROM | disk | FTP | other *** search
Wrap
/* File: SetupGL.c Contains: Functions to enable building and destorying a GL full screen or windowed context Written by: Geoff Stahl (ggs) Copyright: Copyright © 1999 Apple Computer, Inc., All Rights Reserved Change History (most recent first): <10> 12/18/99 ggs Added Windowed creation <9> 11/28/99 ggs Split out DSp and error handling. Added texture memory considerations, assume VRAM is required if other than zero <8> 11/14/99 ggs Fix source server copy <7> 11/13/99 ggs fixed default pixel depth (0) condition that was causing failures <6> 11/13/99 ggs added custom fade code <5> 11/13/99 ggs Reset for Quake 3 use <4> 11/12/99 ggs re-add <3> 11/12/99 ggs added support for frequency retrieval, fixed display number output to be correct if display number input was -1 <2> 11/12/99 ggs 1.0 functionality <1> 11/11/99 ggs Initial Add Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in consideration of your agreement to the following terms, and your use, installation, modification or redistribution of this Apple software constitutes acceptance of these terms. If you do not agree with these terms, please do not use, install, modify or redistribute this Apple software. In consideration of your agreement to abide by the following terms, and subject to these terms, Apple grants you a personal, non-exclusive license, under Apple’s copyrights in this original Apple software (the "Apple Software"), to use, reproduce, modify and redistribute the Apple Software, with or without modifications, in source and/or binary forms; provided that if you redistribute the Apple Software in its entirety and without modifications, you must retain this notice and the following text and disclaimers in all such redistributions of the Apple Software. Neither the name, trademarks, service marks or logos of Apple Computer, Inc. may be used to endorse or promote products derived from the Apple Software without specific prior written permission from Apple. Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by Apple herein, including but not limited to any patent rights that may be infringed by your derivative works or by other works in which the Apple Software may be incorporated. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // system includes ---------------------------------------------------------- // project includes --------------------------------------------------------- #include "Error Handler.h" #include "SetupDSp.h" #include "SetupGL.h" // globals (internal/private) ----------------------------------------------- const RGBColor rgbBlack = { 0x0000, 0x0000, 0x0000 }; // prototypes (internal/private) -------------------------------------------- Boolean CheckRenderer (GDHandle hGD, long *VRAM, long *textureRAM, GLint* pDepthSizeSupport, Boolean fAccelMust); Boolean CheckWindowExtents (GDHandle hGD, short width, short height); void DumpCurrent (AGLDrawable* paglDraw, AGLContext* paglContext, DSpContextReference* pdspContext, pstructGLInfo pcontextInfo); GLenum BuildGLContext (AGLDrawable* paglDraw, AGLContext* paglContext, GDHandle hGD, pstructGLInfo pcontextInfo); OSStatus BuildDrawable (AGLDrawable* paglDraw, GDHandle hGD, pstructGLInfo pcontextInfo); OSStatus BuildDSpContext (DSpContextReference* pdspContext, GDHandle hGD, GLint depthSizeSupport, pstructGLInfo pcontextInfo); OSStatus BuildGLonDevice (AGLDrawable* paglDraw, AGLContext* paglContext, DSpContextReference* pdspContext, GDHandle hGD, pstructGLInfo pcontextInfo); // functions (internal/private) --------------------------------------------- // CheckRenderer // looks at renderer attributes it has at least the VRAM is accelerated // Inputs: hGD: GDHandle to device to look at // pVRAM: pointer to VRAM in bytes required; out is actual VRAM if a renderer was found, otherwise it is the input parameter // pTextureRAM: pointer to texture RAM in bytes required; out is same (implementation assume VRAM returned by card is total so we add texture and VRAM) // fAccelMust: do we check fro acceleration // Returns: true is renderer for the requested device complies, false otherwise; depth buffer bit depths supported Boolean CheckRenderer (GDHandle hGD, long* pVRAM, long* pTextureRAM, GLint* pDepthSizeSupport, Boolean fAccelMust) { AGLRendererInfo info, head_info; GLint inum; GLint dAccel = 0; GLint dVRAM = 0; head_info = aglQueryRendererInfo(&hGD, 1); aglReportError (); if(!head_info) { ReportError ("aglQueryRendererInfo error"); return false; } else { info = head_info; inum = 0; while (info) { aglDescribeRenderer(info, AGL_ACCELERATED, &dAccel); aglReportError (); if (!fAccelMust || dAccel) { aglDescribeRenderer(info, AGL_VIDEO_MEMORY, &dVRAM); // we assume that VRAM returned is total thus add texture and VRAM required aglReportError (); if (dVRAM >= (*pVRAM + *pTextureRAM)) { aglDescribeRenderer(info, AGL_DEPTH_MODES, pDepthSizeSupport); // which depth buffer modes are supported aglReportError (); aglDestroyRendererInfo(head_info); *pVRAM = dVRAM; // return actual VRAM return true; } } info = aglNextRendererInfo(info); aglReportError (); inum++; } } aglDestroyRendererInfo(head_info); // VRAM will remain to same as it did when sent in return false; } //----------------------------------------------------------------------------------------------------------------------- // CheckWindowExtents // looks at renderer attributes it has at least the VRAM is accelerated // Inputs: hGD: GDHandle to device to look at // width/height: requested width and height of window // Returns: true is renderer for the requested device complies, false otherwise Boolean CheckWindowExtents (GDHandle hGD, short width, short height) { Rect strucRect, rectWin = {0, 0, 1, 1}; short deviceHeight = (**hGD).gdRect.bottom - (**hGD).gdRect.top - GetMBarHeight (); short deviceWidth = (**hGD).gdRect.right - (**hGD).gdRect.left; short windowWidthExtra, windowHeightExtra; // build window (not visible) WindowPtr pWindow = NewCWindow (NULL, &rectWin, "\p", true, kWindowDocumentProc, (WindowPtr)-1, 0, 0); strucRect = (**(((WindowPeek)pWindow)->strucRgn)).rgnBBox; windowWidthExtra = (strucRect.right - strucRect.left) - 1; windowHeightExtra = (strucRect.bottom - strucRect.top) - 1; if ((width + windowWidthExtra <= deviceWidth) && (height + windowHeightExtra <= deviceHeight)) { return true; } return false; } // -------------------------------------------------------------------------- // DumpCurrent // Kills currently allocated context // does not care about being pretty (assumes display is liekly faded) // Inputs: paglDraw, paglContext, pdspContext: things to be destroyed void DumpCurrent (AGLDrawable* paglDraw, AGLContext* paglContext, DSpContextReference* pdspContext, pstructGLInfo pcontextInfo) { if (*paglContext) { aglSetCurrentContext (NULL); aglSetDrawable (*paglContext, NULL); aglDestroyContext (*paglContext); *paglContext = NULL; } if (pcontextInfo->fmt) aglDestroyPixelFormat (pcontextInfo->fmt); // pixel format is no longer needed pcontextInfo->fmt = 0; if (*paglDraw) DisposeWindow ((WindowPtr)*paglDraw); *paglDraw = NULL; DestroyDSpContext (pdspContext); } #pragma mark - // -------------------------------------------------------------------------- // BuildGLContext // Builds OpenGL context // Inputs: hGD: GDHandle to device to look at // pcontextInfo: request and requirements for cotext and drawable // Outputs: paglContext as allocated // pcontextInfo: allocated parameters // if fail to allocate: paglContext will be NULL // if error: will return error paglContext will be NULL GLenum BuildGLContext (AGLDrawable* paglDraw, AGLContext* paglContext, GDHandle hGD, pstructGLInfo pcontextInfo) { GLenum err = AGL_NO_ERROR; if ((Ptr) kUnresolvedCFragSymbolAddress == (Ptr) aglChoosePixelFormat) // check for existance of OpenGL { ReportError ("OpenGL not installed"); return NULL; } pcontextInfo->fmt = aglChoosePixelFormat (&hGD, 1, pcontextInfo->aglAttributes); // get an appropriate pixel format aglReportError (); if (NULL == pcontextInfo->fmt) { ReportError("Could not find valid pixel format"); return NULL; } *paglContext = aglCreateContext (pcontextInfo->fmt, NULL); // Create an AGL context aglReportError (); if (NULL == *paglContext) { ReportError ("Could not create context"); return NULL; } // build window as late as possible err = BuildDrawable (paglDraw, hGD, pcontextInfo); if (err != noErr) { ReportError ("Could not build drawable"); return err; } if (!aglSetDrawable (*paglContext, *paglDraw)) // attach the CGrafPtr to the context return aglReportError (); if(!aglSetCurrentContext (*paglContext)) // make the context the current context return aglReportError (); return err; } // -------------------------------------------------------------------------- // BuildDrawable // Builds window to be used as drawable // Inputs: hGD: GDHandle to device to look at // pcontextInfo: request and requirements for cotext and drawable // Outputs: paglDraw as allocated // pcontextInfo: allocated parameters // if fail to allocate: paglDraw will be NULL // if error: will return error paglDraw will be NULL OSStatus BuildDrawable (AGLDrawable* paglDraw, GDHandle hGD, pstructGLInfo pcontextInfo) { Rect rectWin; RGBColor rgbSave; GrafPtr pGrafSave; OSStatus err = noErr; // center window in our context's gdevice rectWin.top = (**hGD).gdRect.top + ((**hGD).gdRect.bottom - (**hGD).gdRect.top) / 2; // v center rectWin.top -= pcontextInfo->height / 2; rectWin.left = (**hGD).gdRect.left + ((**hGD).gdRect.right - (**hGD).gdRect.left) / 2; // h center rectWin.left -= pcontextInfo->width / 2; rectWin.right = rectWin.left + pcontextInfo->width; rectWin.bottom = rectWin.top + pcontextInfo->height; if (pcontextInfo->fFullscreen) *paglDraw = (AGLDrawable) NewCWindow (NULL, &rectWin, "\p", 0, plainDBox, (WindowPtr)-1, 0, 0); else *paglDraw = (AGLDrawable) NewCWindow (NULL, &rectWin, "\p", 0, kWindowDocumentProc, (WindowPtr)-1, 0, 0); ShowWindow ((GrafPtr)*paglDraw); GetPort (&pGrafSave); SetPort ((GrafPtr)*paglDraw); GetForeColor (&rgbSave); RGBForeColor (&rgbBlack); PaintRect (&(*paglDraw)->portRect); RGBForeColor (&rgbSave); // ensure color is reset for proper blitting SetPort (pGrafSave); return err; } // -------------------------------------------------------------------------- // BuildGLonDevice // Takes device single device and tries to build on it // Inputs: hGD: GDHandle to device to look at // *pcontextInfo: request and requirements for cotext and drawable // Outputs: *paglDraw, *paglContext and *pdspContext as allocated // *pcontextInfo: allocated parameters // if fail to allocate: paglDraw, paglContext and pdspContext will be NULL // if error: will return error and paglDraw, paglContext and pdspContext will be NULL OSStatus BuildGLonDevice (AGLDrawable* paglDraw, AGLContext* paglContext, DSpContextReference* pdspContext, GDHandle hGD, pstructGLInfo pcontextInfo) { GLint depthSizeSupport = 0; OSStatus err = noErr; Boolean fCheckRenderer = false; if (pcontextInfo->fFullscreen) { // if we are in 16 or 32 bit mode already, we can check the renderer now (we will double check later) if (16 <= (**(**hGD).gdPMap).pixelSize) { // check for VRAM and accelerated if (!CheckRenderer (hGD, &(pcontextInfo->VRAM), &(pcontextInfo->textureRAM), &depthSizeSupport, pcontextInfo->fAcceleratedMust)) { ReportError ("Renderer check failed"); return err; } else fCheckRenderer = true; } err = BuildDSpContext (pdspContext, hGD, depthSizeSupport, pcontextInfo); if ((err != noErr) || (*pdspContext == NULL)) { if (err != noErr) ReportErrorNum ("BuildDSpContext failed with error:", err); else ReportError ("Could not build DrawSprocket context"); return err; } } else { if (pcontextInfo->pixelDepth == 0) // default { pcontextInfo->pixelDepth = (**(**hGD).gdPMap).pixelSize; if (16 > pcontextInfo->pixelDepth) pcontextInfo->pixelDepth = 16; } if (pcontextInfo->fDepthMust && (pcontextInfo->pixelDepth != (**(**hGD).gdPMap).pixelSize)) // device depth must match and does not { ReportError ("Pixel Depth does not match device in windowed mode."); return err; } if (!CheckWindowExtents (hGD, pcontextInfo->width, pcontextInfo->height)) { ReportError ("Window will not fit on device in windowed mode."); return err; } } // if we have not already checked the renderer, check for VRAM and accelerated if (!fCheckRenderer) if (!CheckRenderer (hGD, &(pcontextInfo->VRAM), &(pcontextInfo->textureRAM), &depthSizeSupport, pcontextInfo->fAcceleratedMust)) { ReportError ("Renderer check failed"); return err; } // do agl err = BuildGLContext (paglDraw, paglContext, hGD, pcontextInfo); // set output parameters return err; } #pragma mark - // functions (public) ------------------------------------------------------- // BuildGL // Takes device and geometry request and tries to build best context and drawable // if device does not work will walk down devices looking for first one that satisfies requirments // Inputs: *pnumDevice: 0 any device, # attempt that device first, then any device // *pcontextInfo: request and requirements for cotext and drawable // Outputs: *paglDraw, *paglContext and *pdspContext as allocated // *pnumDevice to device number in list that was used // *pcontextInfo: allocated parameters // if fail to allocate: paglDraw, paglContext and pdspContext will be NULL // if error: will return error and paglDraw, paglContext and pdspContext will be NULL OSStatus BuildGL (AGLDrawable* paglDraw, AGLContext* paglContext, DSpContextReference* pdspContext, short* pnumDevice, pstructGLInfo pcontextInfo) { GDHandle hGD = NULL; OSStatus err = noErr; structGLInfo contextInfoSave; // clear *paglDraw = 0; *paglContext = 0; *pdspContext = 0; contextInfoSave = *pcontextInfo; // save info to reset on failures if (pcontextInfo->fFullscreen) { err = StartDSp (); if (gDSpStarted) gNeedFade = true; else return err; } //find main device if (*pnumDevice == -1) { GDHandle hDevice; // check number of screens hGD = GetMainDevice (); if (NULL != hGD) { err = BuildGLonDevice (paglDraw, paglContext, pdspContext, hGD, pcontextInfo); // find device number *pnumDevice = 0; hDevice = DMGetFirstScreenDevice (true); do { if (hDevice == hGD) break; hDevice = DMGetNextScreenDevice (hDevice, true); (*pnumDevice)++; } while (hDevice); if (!hDevice) ReportError ("main device match not found"); } else ReportError ("Cannot get main device"); } if ((err != noErr) || (*paglDraw == 0) || (*paglContext == 0)) { *pcontextInfo = contextInfoSave; // restore info //find target device and check this first is one exists if (*pnumDevice) { short i; hGD = DMGetFirstScreenDevice (true); for (i = 0; i < *pnumDevice; i++) { GDHandle hGDNext = DMGetNextScreenDevice (hGD, true); if (NULL == hGDNext) // ensure we did not run out of devices break; // if no more devices drop out else hGD = hGDNext; // otherwise continue } *pnumDevice = i; // record device we actually got err = BuildGLonDevice (paglDraw, paglContext, pdspContext, hGD, pcontextInfo); } } // while we have not allocated a context or there were errors if ((err != noErr) || (*paglDraw == 0) || (*paglContext == 0)) { DumpCurrent (paglDraw, paglContext, pdspContext, pcontextInfo); // dump what ever partial solution we might have *pcontextInfo = contextInfoSave; // restore info // now look through the devices in order hGD = DMGetFirstScreenDevice (true); *pnumDevice = -1; do { (*pnumDevice)++; err = BuildGLonDevice (paglDraw, paglContext, pdspContext, hGD, pcontextInfo); if ((err != noErr) || (*paglDraw == 0) || (*paglContext == 0)) // reset hGD only if we are not done { hGD = DMGetNextScreenDevice (hGD, true); DumpCurrent (paglDraw, paglContext, pdspContext, pcontextInfo); // dump what ever partial solution we might have *pcontextInfo = contextInfoSave; // restore info } } while (((err != noErr) || (*paglDraw == 0) || (*paglContext == 0)) && hGD); } if ((gDSpStarted) && (pcontextInfo->fFullscreen)) { DSpReportError (DSpContext_CustomFadeGammaIn (NULL, NULL, 5)); } return err; } // -------------------------------------------------------------------------- // DestroyGL // Destroys drawable and context // Ouputs: *paglDraw, *paglContext and *pdspContext should be 0 on exit OSStatus DestroyGL (AGLDrawable* paglDraw, AGLContext* paglContext, DSpContextReference* pdspContext, pstructGLInfo pcontextInfo) { if (gDSpStarted) { DSpReportError (DSpContext_CustomFadeGammaOut (NULL, NULL, 5)); } DumpCurrent (paglDraw, paglContext, pdspContext, pcontextInfo); ShutdownDSp (); return noErr; } // -------------------------------------------------------------------------- // BuildGLFromWindow // same as above except that it takes a window as input and attempts to build requested cotext on that OSStatus BuildGLFromWindow (AGLDrawable aglDraw, AGLContext* paglContext, pstructGLWindowInfo pcontextInfo) { GDHandle hGD = NULL; OSStatus err = noErr; structGLWindowInfo contextInfoSave; GLenum glerr = AGL_NO_ERROR; GLint depthSizeSupport = 0; // clear *paglContext = 0; contextInfoSave = *pcontextInfo; // save info to reset on failures if (!CheckRenderer (hGD, &(pcontextInfo->VRAM), &(pcontextInfo->textureRAM), &depthSizeSupport, pcontextInfo->fAcceleratedMust)) { ReportError ("Renderer check failed"); *pcontextInfo = contextInfoSave; // restore info return err; } // do agl if ((Ptr) kUnresolvedCFragSymbolAddress == (Ptr) aglChoosePixelFormat) // check for existance of OpenGL { ReportError ("OpenGL not installed"); *pcontextInfo = contextInfoSave; // restore info return NULL; } pcontextInfo->fmt = aglChoosePixelFormat (NULL, 0, pcontextInfo->aglAttributes); // get an appropriate pixel format aglReportError (); if (NULL == pcontextInfo->fmt) { ReportError("Could not find valid pixel format"); *pcontextInfo = contextInfoSave; // restore info return NULL; } *paglContext = aglCreateContext (pcontextInfo->fmt, NULL); // Create an AGL context aglReportError (); if (NULL == *paglContext) { ReportError ("Could not create context"); *pcontextInfo = contextInfoSave; // restore info return NULL; } if (!aglSetDrawable (*paglContext, aglDraw)) // attach the CGrafPtr to the context { *pcontextInfo = contextInfoSave; // restore info return aglReportError (); } if(!aglSetCurrentContext (*paglContext)) // make the context the current context { *pcontextInfo = contextInfoSave; // restore info return aglReportError (); } return err; } // -------------------------------------------------------------------------- // DestroyGLFromWindow // same as above but destorys a context that was associated with an existing window, window is left intacted OSStatus DestroyGLFromWindow (AGLContext* paglContext, pstructGLWindowInfo pcontextInfo) { if (*paglContext) { aglSetCurrentContext (NULL); aglSetDrawable (*paglContext, NULL); aglDestroyContext (*paglContext); *paglContext = NULL; } if (pcontextInfo->fmt) aglDestroyPixelFormat (pcontextInfo->fmt); // pixel format is no longer needed pcontextInfo->fmt = 0; return noErr; } //----------------------------------------------------------------------------------------------------------------------- // PauseGL // Pauses gl to allow toolbox drawing OSStatus PauseGL (AGLContext aglContext) { glFinish (); // must do this to ensure the queue is complete aglSetDrawable(aglContext, NULL); return aglReportError (); } //----------------------------------------------------------------------------------------------------------------------- // ResumeGL // resumes gl to allow gl drawing OSStatus ResumeGL (AGLDrawable aglDraw, AGLContext aglContext) { aglSetDrawable(aglContext, aglDraw); return aglReportError (); }